package com.manager.service.impl;

import com.manager.bean.PageInfo;
import com.manager.bean.Student;
import com.manager.bean.StudentExample;
import com.manager.mapper.StudentMapper;
import com.manager.service.StudentService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * (Student)表服务实现类
 *
 * @author makejava
 * @since 2021-03-02 11:00:25
 */
@Service("studentService")
public class StudentServiceImpl implements StudentService {
    @Resource
    private StudentMapper studentDao;

    @Override
    public PageInfo<Student> findPageInfo(String name, Integer id, Integer classId, String collegeName, Integer pageIndex, Integer pageSize) {
        PageInfo<Student> pi = new PageInfo<Student>();
        pi.setPageIndex(pageIndex);
        pi.setPageSize(pageSize);
        StudentExample studentExample=new StudentExample();
        StudentExample.Criteria criteria=studentExample.createCriteria();
        if(name!=null&&name!=""){
            criteria.andNameLike("%"+name+"%");
        }
        if(id!=null){
            criteria.andIdEqualTo(id);
        }
        if(classId!=null){
            criteria.andClassIdEqualTo(classId);
        }
        if(collegeName!=null&&collegeName!=""){
            criteria.andCollegeNameEqualTo("%"+collegeName+"%");
        }
        //获取总条数
        Integer totalCount = studentDao.countByExample(studentExample);
        if (totalCount>0){
            pi.setTotalCount(totalCount);
            //每一页显示学生信息数
            //currentPage = (pageIndex-1)*pageSize  当前页码数减1*最大条数=开始行数
            List<Student> studentList =	studentDao.selectByExample(studentExample);
            int start=(pi.getPageIndex()-1)*pi.getPageSize();
            int end=start+pi.getPageSize();
            if(end>studentList.size()-1){
                end=studentList.size();
            }
            studentList=studentList.subList(start,end);
            pi.setList(studentList);
        }
        return pi;
    }

    /**
     * 通过ID查询单条数据
     *
     * @param id 主键
     * @return 实例对象
     */
    @Override
    public Student queryById(Integer id) {
        return this.studentDao.selectByPrimaryKey(id);
    }


    @Override
    public List<Student> getAll() {
        StudentExample studentExample = new StudentExample();
        StudentExample.Criteria criteria = studentExample.createCriteria();
        return studentDao.selectByExample(studentExample);
    }

    /**
     * 新增数据
     *
     * @param student 实例对象
     * @return 实例对象
     */
    @Override
    public Student insert(Student student) {
        this.studentDao.insert(student);
        return student;
    }

    /**
     * 修改数据
     *
     * @param student 实例对象
     * @return 实例对象
     */
    @Override
    public Student update(Student student) {
        this.studentDao.updateByPrimaryKey(student);
        return this.queryById(student.getId());
    }

    /**
     * 通过主键删除数据
     *
     * @param id 主键
     * @return 是否成功
     */
    @Override
    public boolean deleteById(Integer id) {
        return this.studentDao.deleteByPrimaryKey(id) > 0;
    }
}
